package com.fr.cell.editor;

import com.fr.base.FRFont;
import com.fr.base.Style;
import com.fr.base.background.ColorBackground;
import com.fr.cell.Grid;
import com.fr.cell.core.JNumberField;
import com.fr.report.CellElement;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.JTextField;

public class NumberCellEditor extends AbstractCellEditor
{
  private Grid grid;
  private JNumberField numberField;
  private Object oldValue;
  private KeyListener textKeyListener;

  public NumberCellEditor()
  {
    this(new JNumberField());
  }

  public NumberCellEditor(JTextField paramJTextField)
  {
    this.oldValue = "";
    this.textKeyListener = new KeyListener(this)
    {
      private final NumberCellEditor this$0;

      public void keyTyped()
      {
        NumberCellEditor.access$000(this.this$0);
      }

      public void keyPressed()
      {
        int i = paramKeyEvent.getKeyCode();
        if (i == 27)
        {
          NumberCellEditor.access$200(this.this$0).setText("" + NumberCellEditor.access$100(this.this$0));
          paramKeyEvent.consume();
        }
        else if ((i == 10) || (i == 9) || (i == 38) || (i == 40))
        {
          NumberCellEditor.access$300(this.this$0).requestFocus();
          NumberCellEditor.access$300(this.this$0).dispatchEvent(paramKeyEvent);
        }
      }

      public void keyReleased()
      {
      }
    };
    if (paramJTextField instanceof JNumberField)
      this.numberField = ((JNumberField)paramJTextField);
    else
      this.numberField = new JNumberField();
    this.numberField.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 1));
    this.numberField.addKeyListener(this.textKeyListener);
    this.numberField.addFocusListener(new FocusAdapter(this)
    {
      private final NumberCellEditor this$0;

      public void focusLost()
      {
        this.this$0.stopCellEditing();
      }
    });
    this.numberField.setFocusTraversalKeysEnabled(false);
  }

  public int getMaxIntegerLength()
  {
    return this.numberField.getMaxIntegerLength();
  }

  public void setMaxIntegerLength(int paramInt)
  {
    this.numberField.setMaxIntegerLength(paramInt);
  }

  public int getMaxDecimalLength()
  {
    return this.numberField.getMaxDecimalLength();
  }

  public void setMaxDecimalLength(int paramInt)
  {
    this.numberField.setMaxDecimalLength(paramInt);
  }

  public Object getCellEditorValue()
    throws Exception
  {
    if (!(this.numberField.isContentChanged()))
      return this.oldValue;
    double d = this.numberField.getValue();
    if (d == 1.7976931348623157E+308D)
    {
      String str = this.numberField.getTextValue();
      if ((str != null) && (str.trim().length() == 0))
        return str;
      return this.oldValue;
    }
    return new Double(d);
  }

  public Component getCellEditorComponent(Grid paramGrid, CellElement paramCellElement)
  {
    this.grid = paramGrid;
    Object localObject = null;
    if (paramCellElement != null)
      localObject = paramCellElement.getValue();
    this.oldValue = localObject;
    if ((localObject != null) && (localObject instanceof Number))
      this.numberField.setValue(((Number)localObject).doubleValue());
    else
      this.numberField.setText("");
    Style localStyle = null;
    if (paramCellElement != null)
      localStyle = paramCellElement.getStyle();
    adjustTextStyle(paramGrid, localStyle, localObject);
    return this.numberField;
  }

  void adjustTextStyle(Grid paramGrid, Style paramStyle, Object paramObject)
  {
    if (paramStyle == null)
      paramStyle = Style.DEFAULT_STYLE;
    int i = paramStyle.getHorizontalAlignment();
    if (i == 2)
      this.numberField.setHorizontalAlignment(2);
    else if (i == 0)
      this.numberField.setHorizontalAlignment(0);
    else if (i == 4)
      this.numberField.setHorizontalAlignment(4);
    else
      this.numberField.setHorizontalAlignment(2);
    FRFont localFRFont = paramStyle.getFRFont();
    this.numberField.setFont(new Font(localFRFont.getFontName(), localFRFont.getStyle(), localFRFont.getSize()));
    this.numberField.setForeground(paramStyle.getFRFont().getForeground());
    if (paramStyle.getBackground() instanceof ColorBackground)
      this.numberField.setBackground(((ColorBackground)paramStyle.getBackground()).getColor());
    else
      this.numberField.setBackground(Color.white);
  }

  private void adjustTextFieldSize()
  {
    Dimension localDimension1 = this.numberField.getSize();
    Dimension localDimension2 = this.numberField.getPreferredSize();
    this.numberField.setSize((int)Math.max(localDimension1.getWidth(), localDimension2.getWidth()), (int)localDimension1.getHeight());
  }

  static void access$000(NumberCellEditor paramNumberCellEditor)
  {
    paramNumberCellEditor.adjustTextFieldSize();
  }

  static Object access$100(NumberCellEditor paramNumberCellEditor)
  {
    return paramNumberCellEditor.oldValue;
  }

  static JNumberField access$200(NumberCellEditor paramNumberCellEditor)
  {
    return paramNumberCellEditor.numberField;
  }

  static Grid access$300(NumberCellEditor paramNumberCellEditor)
  {
    return paramNumberCellEditor.grid;
  }
}